let App = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
      dataType: 'all',
      list: [],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
      var user_id = wx.getStorageSync("user_id");
      if(!user_id){
          wx.navigateTo({
              url: '/pages/login/login'
          });
      }else{
          this.data.dataType = options.type || 'all';
          this.setData({ dataType: this.data.dataType });
      }
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
      var user_id = wx.getStorageSync("user_id");
      if(!user_id){
          wx.navigateTo({
              url: '/pages/login/login'
          });
      }else{
          // 获取订单列表
          this.getOrderList(this.data.dataType);
      }
  },

  /**
   * 获取订单列表
   */
  getOrderList: function (dataType) {
      let _this = this;
      var page = 1;
      var user_id = wx.getStorageSync("user_id");
          App.acc5ShowLoading(_this)
          App.visit('integral/orderLists?page='+page, {
            'dataType':dataType,user_id:user_id
          }, '',function (result) {
              var lls = _this.data.list;
              if(page==1) {
                  lls = [];
              }
              App.acc5HideLoading(_this)
              console.log("订单列表", result);
              console.log(lls);
              _this.setData({
                  list: lls.concat(result.data.list)
              });
          });
  },

  /**
   * 切换标签
   */
  bindHeaderTap: function (e) {
    this.setData({ dataType: e.target.dataset.type });
    // 获取订单列表
    this.getOrderList(e.target.dataset.type);
  },

  /**
   * 取消订单
   */
  cancelOrder: function (e) {
      let _this = this;
      let order_id = e.currentTarget.dataset.id;
      var user_id = wx.getStorageSync("user_id");
      wx.showModal({
          title: "提示",
          content: "确认取消订单？",
          success: function (o) {
              if (o.confirm) {
                  App.visit('good/cancel', {
                    order_id:order_id,user_id:user_id
                  }, '',function (result) {
                      _this.getOrderList(_this.data.dataType);
                  });
              }
          }
      });
  },

  /**
   * 确认收货
   */
  receipt: function (e) {
      let _this = this;
      let order_id = e.currentTarget.dataset.id;
      var user_id = wx.getStorageSync("user_id");
      wx.showModal({
          title: "提示",
          content: "确认收到商品？",
          success: function (o) {
              if (o.confirm) {
                  App.visit('integral/receipt', { order_id:order_id,user_id:user_id }, '',function (result) {
                      _this.getOrderList(_this.data.dataType);
                  });
              }
          }
      });
  },

  /**
   * 发起付款
   */
  payOrder: function (e) {
      let _this = this;
      let order_id = e.currentTarget.dataset.id;
      var user_id = wx.getStorageSync("user_id");
      // 显示loading
      wx.showLoading({ title: '正在处理...', });
      App.visit('good/pay', {
        order_id:order_id,user_id:user_id
      }, 'POST',function (result) {
          console.error('支付',result);
          // 发起微信支付
          wx.requestPayment({
              timeStamp: result.data.timeStamp,
              nonceStr: result.data.nonceStr,
              package: 'prepay_id=' + result.data.prepay_id,
              signType: 'MD5',
              paySign: result.data.paySign,
              success: function (res) {
                  wx.navigateTo({
                      url: '../order/detail?order_id=' + order_id
                  });
              }
          });
          wx.hideLoading();
      });
  },

    /**
     * 跳转订单详情页
     */
    detail: function (e) {
        let order_id = e.currentTarget.dataset.id;
        wx.navigateTo({
            url: '../order/jf_detail?order_id=' + order_id
        });
    },

  onPullDownRefresh: function () {
    wx.stopPullDownRefresh();
  }


});